package com.ttx.ch05;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class ReduceTest {

    public static void main(String[] args) {
        System.out.println("1----------------");
        test1();
        System.out.println("2----------------");
        test2();
        System.out.println("3----------------");
        test3();
        System.out.println("4----------------");
        test4();
        System.out.println("5----------------");
        test5();
    }

    private static void test1() {
        final List<Integer> integers = Arrays.asList(1, 3, 5, 2, 6, 8);
        final Integer sum = integers.stream().reduce(0, (a, b) -> a + b);
        System.out.println(sum);
    }

    private static void test2() {
        final List<Integer> integers = Arrays.asList(1, 3, 5, 2, 6, 8);
        final Integer sum = integers.stream().mapToInt(i -> i).sum();
        System.out.println(sum);
    }

    private static void test3() {
        final List<Integer> integers = Arrays.asList(1, 3, 5, 2, 6, 8);
        final IntStream intStream = integers.stream().mapToInt(i -> i);
        final Stream<Integer> boxedStream = intStream.boxed();
    }

    private static void test4() {
        IntStream.rangeClosed(1, 100).boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 100)
                        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                        .mapToObj(c -> new int[]{a, c, (int) Math.sqrt(a * a + c * c)})
                ).map(d -> "<" + d[0] + "," + d[1] + "," + d[2] + ">")
                .forEach(System.out::println);
    }

    private static void test5() {
        IntStream.rangeClosed(1, 100).boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 100)
                        .mapToObj(b -> new double[]{a, b, Math.sqrt(a * a + b * b)})
                        .filter(t -> t[2] % 1 == 0))
                .forEach(x -> System.out.println((int) x[0] + "," + (int) x[1] + "," + (int) x[2]));
    }
}